/**
 * Licensed under the MIT License
 *
 * Copyright (c) 2012 Gary Paluk, http://www.plugin.io
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * http://www.opensource.org/licenses/mit-license.php
 */

package io.plugin.blib.gfx 
{
	import flash.geom.Point;
	import flash.geom.Rectangle;
	/**
	 * ...
	 * @author Gary Paluk - http://www.plugin.io
	 */
	public class BlibTileSet
	{
		
		private var _texture: BlibTexture;
		private var _tileWidth: int = -1;
		private var _tileHeight: int = -1;
		
		private var GFX: BlibDisplay;
		
		public function BlibTileSet( texture: BlibTexture, tileWidth: uint = 0, tileHeight: uint = 0 ) 
		{
			GFX = BlibDisplay.getInstance();
			
			_texture = texture;
			tileWidth == 0 ? _tileWidth = texture.width : _tileWidth = tileWidth;
			tileHeight == 0 ? _tileHeight = texture.height : _tileHeight = tileHeight;
		}
		
		public function quickDraw( tileX: int, tileY: int, x: Number, y: Number ): void
		{
			
			// TODO swap memory using Alchemy and use quads and texture references sent by pointer
			var tx: Number = tileX * _tileWidth;
			var ty: Number = tileY * _tileHeight;
			
			GFX.bitmapData.lock();
			GFX.bitmapData.copyPixels( _texture.bitmapData, new Rectangle( tx, ty, _tileWidth, _tileHeight ), new Point( x, y ) );
			GFX.bitmapData.unlock();
			
		}
		
		public function get numTiles(): int
		{
			return tilesWide * tilesTall;
		}
		
		public function getTile( x: int, y: int ): BlibTile
		{
			return new BlibTile( this, x, y );
		}
		
		public function getTileAt( index: int ): BlibTile
		{
			var wide: int = tilesWide;
			var x: int = index % wide;
			return new BlibTile( this, x, ( index - x ) / wide );
		}
		
		public function get tilesWide(): int
		{
			return _texture.width / _tileWidth;
		}
		
		public function set tilesWide( value: int ): void
		{
			_tileWidth = _texture.width / value;
		}
		
		public function get tilesTall(): int
		{
			return _texture.height / _tileHeight;
		}
		
		public function set tilesTall( value: int ): void
		{
			_tileHeight = _texture.height / value;
		}
		
		public function get tileWidth():int 
		{
			return _tileWidth;
		}
		
		public function set tileWidth(value:int):void 
		{
			_tileWidth = value;
		}
		
		public function get tileHeight():int 
		{
			return _tileHeight;
		}
		
		public function set tileHeight(value:int):void 
		{
			_tileHeight = value;
		}
		
		public function get texture():BlibTexture 
		{
			return _texture;
		}
		
		public function set texture(value:BlibTexture):void 
		{
			_texture = value;
		}
		
	}

}